#include <bsl_err_internal.h>
#include <bsl_params.h>
#include <crypt_algid.h>
#include <crypt_eal_implprovider.h>
#include <crypt_eal_provider.h>
#include <crypt_errno.h>

#include "kaev1_modes.h"

static void *KAE_Mode_DefaultNewCtx(void *provCtx, int32_t algId)
{
    (void)provCtx;
    void *modeCtx = NULL;
    switch (algId) {
        case CRYPT_CIPHER_SM4_ECB:
        case CRYPT_CIPHER_SM4_CBC:
        case CRYPT_CIPHER_SM4_CTR:
        case CRYPT_CIPHER_SM4_XTS:
        case CRYPT_CIPHER_SM4_OFB:
        case CRYPT_CIPHER_AES128_ECB:
        case CRYPT_CIPHER_AES128_CBC:
        case CRYPT_CIPHER_AES128_CTR:
        case CRYPT_CIPHER_AES128_OFB:
        case CRYPT_CIPHER_AES192_ECB:
        case CRYPT_CIPHER_AES192_CBC:
        case CRYPT_CIPHER_AES192_CTR:
        case CRYPT_CIPHER_AES192_OFB:
        case CRYPT_CIPHER_AES256_ECB:
        case CRYPT_CIPHER_AES256_CBC:
        case CRYPT_CIPHER_AES256_CTR:
        case CRYPT_CIPHER_AES256_OFB:
            modeCtx = KAE_Mode_NewCtx(algId);
            break;
    }
    if (modeCtx == NULL) {
        BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT);
        return NULL;
    }
    return modeCtx;
}

const CRYPT_EAL_Func kaeMode[] = {
    {CRYPT_EAL_IMPLCIPHER_NEWCTX, KAE_Mode_DefaultNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, KAE_Mode_InitCtx},
    {CRYPT_EAL_IMPLCIPHER_UPDATE, KAE_Mode_Update},        {CRYPT_EAL_IMPLCIPHER_FINAL, KAE_Mode_Final},
    {CRYPT_EAL_IMPLCIPHER_DEINITCTX, KAE_Mode_Deinit},     {CRYPT_EAL_IMPLCIPHER_CTRL, KAE_Mode_Ctrl},
    {CRYPT_EAL_IMPLCIPHER_FREECTX, KAE_Mode_Free},         CRYPT_EAL_FUNC_END,
};